#!/usr/bin/env python
# -*- coding: utf-8 -*-

import pywinauto
from pywinauto import clipboard,keyboard
from applications.trade.server.captcha_recognize import captcha_recognize
import pandas as pd
import io
import time
import datetime
import sys

if __name__ == "__main__":

    app = pywinauto.Application().connect(path=r"D:\Program\tonghuashun\xiadan.exe", timeout=2)


    """ 获取grid里面的数据 """
    try:
        app.top_window().window(control_id=0x417, class_name='CVirtualGridCtrl').set_focus()
    except:
        time.sleep(0.5)
        app.top_window().window(control_id=0x417, class_name='CVirtualGridCtrl').set_focus()

    #keyboard.send_keys('{VK_APPS}')  # 键盘鼠标右键
    #time.sleep(1)
    #keyboard.send_keys('c')  # 发送c 软件中的复制功能通过右键实现(ctrl+c快捷键不起作用)
    keyboard.send_keys('^a^c')  # 发送c 软件中的复制功能通过右键实现(ctrl+c快捷键不起作用)
    time.sleep(0.01)

    if app.top_window().child_window(control_id=0x965, class_name='Static').exists():
        file_path = "tmp.png"
        # 保存验证码
        app.top_window().child_window(control_id=0x965, class_name='Static').capture_as_image().save(file_path)

        for i in range(5):
            if not (app.top_window().child_window(control_id=0x965, class_name='Static').exists()):
                break
            if i > 0:
                app.top_window().child_window(control_id=0x964, class_name='Edit').type_keys('{BACKSPACE}' * 4)


            captcha_num = captcha_recognize(file_path)  # 识别验证码
            print("captcha result-->", captcha_num)
            # 模拟输入验证码
            app.top_window().child_window(control_id=0x964, class_name='Edit').type_keys(captcha_num)

            app.top_window().set_focus()
            keyboard.send_keys("{ENTER}")  # 模拟发送enter，点击确定

    data = clipboard.GetData()  # 读取剪贴板数据
    print(data)
    df = pd.read_csv(io.StringIO(data), delimiter='\t', na_filter=False)
    print('-----------------------------------------------------------')
    print(df.to_dict('records'))